package com.aldjob.controllerImpl;

import java.util.Date;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.tools.ProjectTools;


public class UserController {
	private tbl_sysuserMapper tbl_sysuserMapper;
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	
	public UserController(com.project.XXXX.inter.tbl_sysuserMapper tbl_sysuserMapper,
			com.project.XXXX.inter.tbl_employeeinfoMapper tbl_employeeinfoMapper,
			com.project.XXXX.inter.tbl_employerinfoMapper tbl_employerinfoMapper) {
		super();
		this.tbl_sysuserMapper = tbl_sysuserMapper;
		this.tbl_employeeinfoMapper = tbl_employeeinfoMapper;
		this.tbl_employerinfoMapper = tbl_employerinfoMapper;
//		System.out.println("usercontroller constructer called!");
//		System.out.println("tbl_sysuserMapper"+tbl_sysuserMapper);
	}

	public int register(String PhoneNumber, String Password, String validateCode, HttpSession session) {
		String code = "";
		String phoneNumber = "";
		if(session != null){
			code = (String) session.getAttribute("validateCode"); 
			phoneNumber = (String) session.getAttribute("phoneNumber");
		}
		if (StringUtils.equals(validateCode, code) && StringUtils.equals(phoneNumber, phoneNumber)) {
			if (tbl_sysuserMapper.selectByPrimaryKey(PhoneNumber) == null) {
				tbl_sysuser record;
				tbl_employeeinfo EErecord;
				tbl_employerinfo ERrecord;
				record = new tbl_sysuser();
				record.setMobile(PhoneNumber);
				record.setPwd(Password);
				record.setCreatetime(new Date());
				ERrecord = new tbl_employerinfo();
				EErecord = new tbl_employeeinfo();
				ERrecord.setMobile(PhoneNumber);
				EErecord.setMobile(PhoneNumber);
				EErecord.setCreatetime(new Date());
				ERrecord.setCreatetime(new Date());
				ERrecord.setPwd(Password);
				ERrecord.setPwd(Password);
				tbl_sysuserMapper.insertSelective(record);
				tbl_employerinfoMapper.insertSelective(ERrecord);
				tbl_employeeinfoMapper.insertSelective(EErecord);
				session.setAttribute("User", record);
				session.setMaxInactiveInterval(900);
				return 200;
			} else {
				return 409;
			}
		} else {
			return 403;
		}
	}

	public int getMessage(String phoneNum, HttpSession session) {
		//System.out.println("tbl_sysuserMapper"+tbl_sysuserMapper);
		tbl_sysuser record = tbl_sysuserMapper.selectByPrimaryKey(phoneNum);
		String validateCode = null;
		if (record == null) {
			try {
				validateCode = ProjectTools.getMessage(phoneNum);
				session.setAttribute("validateCode", validateCode);
				session.setAttribute("phoneNumber", phoneNum);
			} catch (Exception e) {
				return 403;
			}
			return 200;
		} 
		else
			return 409;
	}

	public int LoginHandler(String PhoneNumber, String Password,HttpSession session) {
		tbl_sysuser record = (tbl_sysuser) tbl_sysuserMapper.selectByPrimaryKey(PhoneNumber);
		if (record == null){
			return 403;
			
		}
		else if (record.getPwd().equals(Password)) {
			session.setAttribute("User", record);
			tbl_employerinfo	ERrecord = (tbl_employerinfo) tbl_employerinfoMapper.selectByPrimaryKey(PhoneNumber);
			tbl_employeeinfo	EErecord = (tbl_employeeinfo) tbl_employeeinfoMapper.selectByPrimaryKey(PhoneNumber);
			session.setAttribute("EEinfo", EErecord);
			session.setAttribute("ERinfo", ERrecord);
			session.setMaxInactiveInterval(-1);
			return 200;
		} else 
			return 403;

	}
}